package com.alibaba.citrus.cr.mn.order.center.facade.repository.impl;

import com.alibaba.citrus.cr.common.constant.OrderFeaturesKey;
import com.alibaba.citrus.cr.mn.order.center.facade.repository.ForecastOrderLineWriteRepository;
import com.alibaba.citrus.cr.mn.order.center.facade.repository.ForecastOrderWriteRepository;
import com.alibaba.cz.base.tool.log.Log;
import com.alibaba.fastjson.JSON;
import com.epoch.app.crforecastsales.domian.forecastorder.dto.BatchUpdateStatusByIdsRequest;
import com.epoch.app.crforecastsales.domian.forecastorder.dto.CreateForecastOrderResponse;
import com.epoch.app.crforecastsales.domian.forecastorder.dto.LoadForecastOrderRequest;
import com.epoch.app.crforecastsales.domian.forecastorder.dto.UpdateForecastOrderResponse;
import com.epoch.app.crforecastsales.domian.forecastorder.model.ForecastOrder;
import com.epoch.app.crforecastsales.domian.forecastorder.service.ForecastOrderService;
import com.epoch.app.crforecastsales.enums.ForecastStatusEnum;
import com.epoch.app.crforecastsales.sdo.ForecastOrderLineSDO;
import com.epoch.app.crforecastsales.sdo.ForecastOrderSDO;
import com.epoch.app.mnordercenter.model.dto.Modifiers;
import com.epoch.app.mnordercenter.model.dto.SubmitForecastResponseDTO;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service("forecastOrderWriteRepositoryBiz")
public class ForecastOrderWriteRepositoryImpl implements ForecastOrderWriteRepository {
    private static final Log log = Log.getLogger(ForecastOrderWriteRepositoryImpl.class);
    @Resource
    private ForecastOrderService forecastOrderService;
    @Resource
    private ForecastOrderLineWriteRepository forecastOrderLineWriteRepository;

    @Override
    public String save(ForecastOrderSDO forecastOrderSDO) {
        //保存主单 TODO 业务渠道与销售渠道的关系  客户地址与客户的级联关系
        ForecastOrder forecastOrder = new ForecastOrder();
        forecastOrder.setOuterOrderId(forecastOrderSDO.getOuterOrderId());
        forecastOrder.setOrderBizId(forecastOrderSDO.getOrderBizId());
        forecastOrder.setAreaCode(forecastOrderSDO.getAreaCode());
        forecastOrder.setBisUnitCode(forecastOrderSDO.getBisUnitCode());
        forecastOrder.setSalesmanId(forecastOrderSDO.getSalesmanCode());
        forecastOrder.setSalesmanName(forecastOrderSDO.getSalesmanName());
        //客户负责人 add by myw,20220928
        forecastOrder.setCustomerPrincipalId(forecastOrderSDO.getCustomerPrincipalId());
        forecastOrder.setCustomerPrincipalName(forecastOrderSDO.getCustomerPrincipalName());
        forecastOrder.setStatus(forecastOrderSDO.getStatus());
        forecastOrder.setOrderTag(forecastOrderSDO.getOrderTag());
        forecastOrder.setOrderType(forecastOrderSDO.getOrderType());
        if (StringUtils.isNotBlank(forecastOrderSDO.getId())) {
            forecastOrder.setId(Long.parseLong(forecastOrderSDO.getId()));
        }
        forecastOrder.setReceiveAddressCode(forecastOrderSDO.getReceiveAddressCode());
        forecastOrder.setReceiveDetailAddress(forecastOrderSDO.getReceiveDetailAddress());
        forecastOrder.setCustomerCode(forecastOrderSDO.getCustomerCode());
        forecastOrder.setCustomerName(forecastOrderSDO.getCustomerName());

        forecastOrder.setShipToPartyCode(forecastOrderSDO.getReceiveAddressCode());
        forecastOrder.setShipToPartyName(forecastOrderSDO.getReceiveDetailAddress());
        forecastOrder.setSoldToPartyCode(forecastOrderSDO.getCustomerCode());
        forecastOrder.setSoldToPartyName(forecastOrderSDO.getCustomerName());

        forecastOrder.setTransportModeCode(forecastOrderSDO.getTransportModeCode());
        forecastOrder.setTransportModeName(forecastOrderSDO.getTransportModeName());
        forecastOrder.setChannelCode(forecastOrderSDO.getChannelCode());
        forecastOrder.setChannelName(forecastOrderSDO.getChannelName());
        forecastOrder.setDepartmentCode(forecastOrderSDO.getDepartmentCode());
        forecastOrder.setDepartmentName(forecastOrderSDO.getDepartmentName());
        forecastOrder.setOrganizationCode(forecastOrderSDO.getOrganizationCode());
        forecastOrder.setOrganizationName(forecastOrderSDO.getOrganizationName());
        forecastOrder.setSaleChannelCode(forecastOrderSDO.getSaleChannelCode());
        forecastOrder.setSaleChannelName(forecastOrderSDO.getSaleChannelName());
        forecastOrder.setCreditBalance(forecastOrderSDO.getCreditBalance());
        forecastOrder.setProductGroupCode(forecastOrderSDO.getProductGroupCode());
        forecastOrder.setProductGroupName(forecastOrderSDO.getProductGroupName());
        forecastOrder.setCreateReason(forecastOrderSDO.getCreateReason());
        forecastOrder.setCreateReasonName(forecastOrderSDO.getCreateReasonName());
        //仓库
        forecastOrder.setWareHouseCode(forecastOrderSDO.getWareHouseCode());
        forecastOrder.setWareHouseName(forecastOrderSDO.getWareHouseName());
        forecastOrder.setShippingWarehouseId(forecastOrderSDO.getShippingWarehouseId());
        forecastOrder.setShippingWarehouseCode(forecastOrderSDO.getShippingWarehouseCode());
        forecastOrder.setShippingWarehouseName(forecastOrderSDO.getShippingWarehouseName());
        forecastOrder.setDeliveryFactoryCode(forecastOrderSDO.getDeliveryFactoryCode());
        forecastOrder.setStockCode(forecastOrderSDO.getStockCode());

        forecastOrder.setCustomerReferenceNo(forecastOrderSDO.getCustomerReferenceNo());
        forecastOrder.setRemark(forecastOrderSDO.getRemark());
        forecastOrder.setVaildBeginTime(forecastOrderSDO.getVaildBeginTime());
        forecastOrder.setBizCode("com.mn.demo");
        forecastOrder.setBusinessCode("com.base.line");
        //创建人
        forecastOrder.setCreatorId(forecastOrderSDO.getCreatorId());
        forecastOrder.setCreatorCode(forecastOrderSDO.getCreatorCode());
        forecastOrder.setCreatorName(forecastOrderSDO.getCreatorName());
        forecastOrder.setModifierId(forecastOrderSDO.getCreatorId());
        forecastOrder.setModifierCode(forecastOrderSDO.getModifierCode());
        forecastOrder.setModifierName(forecastOrderSDO.getCreatorName());
        //联系人
        forecastOrder.setContact(forecastOrderSDO.getContact());
        forecastOrder.setContactPhone(forecastOrderSDO.getContactPhone());
        forecastOrder.setCustomerExpectTDate(forecastOrderSDO.getCustomerExpectTDate());
        forecastOrder.setCustomerPurchaseDate(forecastOrderSDO.getCustomerPurchaseDate());
        forecastOrder.setCustomerPurchaseOrderNo(forecastOrderSDO.getCustomerPurchaseOrderNo());
        forecastOrder.setGmtCreate(forecastOrderSDO.getGmtCreate());
        //组织信息
        forecastOrder.setSalesOrganizationId(forecastOrderSDO.getSalesOrganizationId());
        forecastOrder.setSalesOrganizationName(forecastOrderSDO.getSalesOrganizationName());
        forecastOrder.setSalesOrganizationCode(forecastOrderSDO.getSalesOrganizationCode());

        forecastOrder.setSalesChannelId(forecastOrderSDO.getSalesChannelId());
        forecastOrder.setSalesChannelName(forecastOrderSDO.getSalesChannelName());
        forecastOrder.setSalesChannelCode(forecastOrderSDO.getSalesChannelCode());

        forecastOrder.setSalesDepartmentId(forecastOrderSDO.getSalesDepartmentId());
        forecastOrder.setSalesDepartmentName(forecastOrderSDO.getSalesDepartmentName());
        forecastOrder.setSalesDepartmentCode(forecastOrderSDO.getSalesDepartmentCode());
        //业态信息
        forecastOrder.setFromSys(forecastOrderSDO.getFromSys());
        forecastOrder.setBusinessType(forecastOrderSDO.getBusinessType());
        forecastOrder.setBusinessTypeName(forecastOrderSDO.getBusinessTypeName());
        forecastOrder.setBusinessUnitCode(forecastOrderSDO.getBusinessUnitCode());
        forecastOrder.setBusinessUnitName(forecastOrderSDO.getBusinessUnitName());
        forecastOrder.setOrderTypeName(forecastOrderSDO.getOrderTypeName());
        forecastOrder.setOrderTypeCode(forecastOrderSDO.getOrderTypeCode());
        forecastOrder.setSourceOrderNo(forecastOrderSDO.getSourceOrderNo());
        forecastOrder.setBizFeatures(forecastOrderSDO.getBizFeatures());
        forecastOrder.setReqNo(forecastOrderSDO.getReqNo());
        forecastOrder.setSyncVersion(1);

        Map<String, String> features = forecastOrderSDO.getFeatures();
        if (MapUtils.isEmpty(features)) {
            features = new HashMap<>();
        }
        features.put(OrderFeaturesKey.KA_AREA, forecastOrderSDO.getKaArea());
        features.put(OrderFeaturesKey.KA_SYSTEM, forecastOrderSDO.getKaSystem());
        if (Objects.nonNull(forecastOrderSDO.getSourceOrderDate())) {
            features.put(OrderFeaturesKey.SOURCE_ORDER_DATE, String.valueOf(forecastOrderSDO.getSourceOrderDate().getTime()));
        }
        forecastOrder.setFeatures(JSON.toJSONString(features));

        // 一盘货新增字段
        forecastOrder.setOrderCategoryCode(forecastOrderSDO.getOrderCategoryCode());
        forecastOrder.setOrderChannelCode(forecastOrderSDO.getOrderChannelCode());
        forecastOrder.setReceiveWarehouseCode(forecastOrderSDO.getReceiveWarehouseCode());
        forecastOrder.setReceiveWarehouseName(forecastOrderSDO.getReceiveWarehouseName());
        forecastOrder.setReceiveFactoryCode(forecastOrderSDO.getReceiveFactoryCode());
        forecastOrder.setReceiveStockCode(forecastOrderSDO.getReceiveStockCode());
        forecastOrder.setFulfillmentTypeCode(forecastOrderSDO.getFulfillmentTypeCode());
        forecastOrder.setFulfillmentPriority(forecastOrderSDO.getFulfillmentPriority());
        forecastOrder.setAutoFulfillment(forecastOrderSDO.getAutoFulfillment());
        forecastOrder.setCustomerGrade(forecastOrderSDO.getCustomerGrade());
        forecastOrder.setCustomerGroup(forecastOrderSDO.getCustomerGroup());
        forecastOrder.setCustomerGroupName(forecastOrderSDO.getCustomerGroupName());
        forecastOrder.setVaildBeginTime(forecastOrderSDO.getVaildBeginTime());
        forecastOrder.setValidEndTime(forecastOrderSDO.getValidEndTime());
        CreateForecastOrderResponse createForecastOrderResponse = forecastOrderService.createForecastOrder(forecastOrder);
        String lastInsertId = createForecastOrderResponse.getLastInsertId();
        if (StringUtils.isEmpty(lastInsertId)) {
            log.error("ForecastOrderWriteRepositoryImpl#save_fail");
        }
        return lastInsertId;
    }

    @Override
    public Boolean modify(ForecastOrderSDO forecastOrderSDO) {
        ForecastOrder forecastOrder = new ForecastOrder();
        forecastOrder.setId(Long.parseLong(forecastOrderSDO.getId()));
        if (StringUtils.isNotBlank(forecastOrderSDO.getReceiveAddressCode())) {
            forecastOrder.setReceiveAddressCode(forecastOrderSDO.getReceiveAddressCode());
            forecastOrder.setShipToPartyCode(forecastOrderSDO.getShipAddressCode());
        }
        if (StringUtils.isNotBlank(forecastOrderSDO.getReceiveDetailAddress())) {
            forecastOrder.setReceiveDetailAddress(forecastOrderSDO.getReceiveDetailAddress());
            forecastOrder.setShipToPartyName(forecastOrderSDO.getReceiveDetailAddress());
        }

        if (StringUtils.isNotBlank(forecastOrderSDO.getCustomerCode())) {
            forecastOrder.setCustomerCode(forecastOrderSDO.getCustomerCode());
            forecastOrder.setSoldToPartyCode(forecastOrderSDO.getCustomerCode());
        }
        if (StringUtils.isNotBlank(forecastOrderSDO.getCustomerName())) {
            forecastOrder.setCustomerName(forecastOrderSDO.getCustomerName());
            forecastOrder.setSoldToPartyName(forecastOrderSDO.getCustomerName());
        }
        if (StringUtils.isNotBlank(forecastOrderSDO.getTransportModeCode())) {
            forecastOrder.setTransportModeCode(forecastOrderSDO.getTransportModeCode());
        }
        if (StringUtils.isNotBlank(forecastOrderSDO.getTransportModeName())) {
            forecastOrder.setTransportModeName(forecastOrderSDO.getTransportModeName());
        }
        if (StringUtils.isNotBlank(forecastOrderSDO.getChannelCode())) {
            forecastOrder.setChannelCode(forecastOrderSDO.getChannelCode());
        }
        if (StringUtils.isNotBlank(forecastOrderSDO.getChannelName())) {
            forecastOrder.setChannelName(forecastOrderSDO.getChannelName());
        }
        if (StringUtils.isNotBlank(forecastOrderSDO.getDepartmentCode())) {
            forecastOrder.setDepartmentCode(forecastOrderSDO.getDepartmentCode());
        }
        if (StringUtils.isNotBlank(forecastOrderSDO.getDepartmentName())) {
            forecastOrder.setDepartmentName(forecastOrderSDO.getDepartmentName());
        }

        if (StringUtils.isNotBlank(forecastOrderSDO.getOrganizationCode())) {
            forecastOrder.setOrganizationCode(forecastOrderSDO.getOrganizationCode());
        }
        if (StringUtils.isNotBlank(forecastOrderSDO.getOrganizationName())) {
            forecastOrder.setOrganizationName(forecastOrderSDO.getOrganizationName());
        }

        if (StringUtils.isNotBlank(forecastOrderSDO.getSalesOrganizationCode())) {
            forecastOrder.setSalesOrganizationCode(forecastOrderSDO.getSalesOrganizationCode());
        }
        if (StringUtils.isNotBlank(forecastOrderSDO.getSalesOrganizationId())) {
            forecastOrder.setSalesOrganizationId(forecastOrderSDO.getSalesOrganizationId());
        }
        if (StringUtils.isNotBlank(forecastOrderSDO.getSalesOrganizationName())) {
            forecastOrder.setSalesOrganizationName(forecastOrderSDO.getSalesOrganizationName());
        }
        if (StringUtils.isNotBlank(forecastOrderSDO.getSalesChannelCode())) {
            forecastOrder.setSalesChannelCode(forecastOrderSDO.getSalesChannelCode());
        }
        if (StringUtils.isNotBlank(forecastOrderSDO.getSalesChannelId())) {
            forecastOrder.setSalesChannelId(forecastOrderSDO.getSalesChannelId());
        }
        if (StringUtils.isNotBlank(forecastOrderSDO.getSalesChannelName())) {
            forecastOrder.setSalesChannelName(forecastOrderSDO.getSalesChannelName());
        }
        if (StringUtils.isNotBlank(forecastOrderSDO.getSalesDepartmentCode())) {
            forecastOrder.setSalesDepartmentCode(forecastOrderSDO.getSalesDepartmentCode());
        }
        if (StringUtils.isNotBlank(forecastOrderSDO.getSalesDepartmentId())) {
            forecastOrder.setSalesDepartmentId(forecastOrderSDO.getSalesDepartmentId());
        }
        if (StringUtils.isNotBlank(forecastOrderSDO.getSalesDepartmentName())) {
            forecastOrder.setSalesDepartmentName(forecastOrderSDO.getSalesDepartmentName());
        }
        if (StringUtils.isNotBlank(forecastOrderSDO.getShippingWarehouseCode())) {
            forecastOrder.setShippingWarehouseCode(forecastOrderSDO.getShippingWarehouseCode());
        }
        if (StringUtils.isNotBlank(forecastOrderSDO.getShippingWarehouseId())) {
            forecastOrder.setShippingWarehouseId(forecastOrderSDO.getShippingWarehouseId());
        }
        if (StringUtils.isNotBlank(forecastOrderSDO.getShippingWarehouseName())) {
            forecastOrder.setShippingWarehouseName(forecastOrderSDO.getShippingWarehouseName());
        }
        if (StringUtils.isNotBlank(forecastOrderSDO.getStockCode())) {
            forecastOrder.setStockCode(forecastOrderSDO.getStockCode());
        }
        if (StringUtils.isNotBlank(forecastOrderSDO.getDeliveryFactoryCode())) {
            forecastOrder.setDeliveryFactoryCode(forecastOrderSDO.getDeliveryFactoryCode());
        }
        if (StringUtils.isNotBlank(forecastOrderSDO.getCreditBalance())) {
            forecastOrder.setCreditBalance(forecastOrderSDO.getCreditBalance());
        }
        if (StringUtils.isNotBlank(forecastOrderSDO.getProductGroupCode())) {
            forecastOrder.setProductGroupCode(forecastOrderSDO.getProductGroupCode());
        }
        if (StringUtils.isNotBlank(forecastOrderSDO.getProductGroupName())) {
            forecastOrder.setProductGroupName(forecastOrderSDO.getProductGroupName());
        }
        if (StringUtils.isNotBlank(forecastOrderSDO.getCreateReason())) {
            forecastOrder.setCreateReason(forecastOrderSDO.getCreateReason());
        }
        if (StringUtils.isNotBlank(forecastOrderSDO.getCreateReasonName())) {
            forecastOrder.setCreateReasonName(forecastOrderSDO.getCreateReasonName());
        }
        if (StringUtils.isNotBlank(forecastOrderSDO.getWareHouseCode())) {
            forecastOrder.setWareHouseCode(forecastOrderSDO.getWareHouseCode());
        }
        if (StringUtils.isNotBlank(forecastOrderSDO.getWareHouseName())) {
            forecastOrder.setWareHouseName(forecastOrderSDO.getWareHouseName());
        }
        if (StringUtils.isNotBlank(forecastOrderSDO.getShippingWarehouseId())) {
            forecastOrder.setShippingWarehouseId(forecastOrderSDO.getShippingWarehouseId());
        }
        if (StringUtils.isNotBlank(forecastOrderSDO.getShippingWarehouseName())) {
            forecastOrder.setShippingWarehouseName(forecastOrderSDO.getShippingWarehouseName());
        }
        if (StringUtils.isNotBlank(forecastOrderSDO.getShippingWarehouseCode())) {
            forecastOrder.setShippingWarehouseCode(forecastOrderSDO.getShippingWarehouseCode());
        }
        if (StringUtils.isNotBlank(forecastOrderSDO.getCustomerReferenceNo())) {
            forecastOrder.setCustomerReferenceNo(forecastOrderSDO.getCustomerReferenceNo());
        }
        forecastOrder.setRemark(forecastOrderSDO.getRemark());
        forecastOrder.setAddRemark(forecastOrderSDO.getAddRemark());
        if (null != forecastOrderSDO.getCustomerExpectTDate()) {
            forecastOrder.setCustomerExpectTDate(forecastOrderSDO.getCustomerExpectTDate());
        }
        if (null != forecastOrderSDO.getVaildBeginTime()) {
            forecastOrder.setVaildBeginTime(forecastOrderSDO.getVaildBeginTime());
        }
        if (StringUtils.isNotBlank(forecastOrderSDO.getModifierId())) {
            forecastOrder.setModifierId(forecastOrderSDO.getModifierId());
        }
        if (StringUtils.isNotBlank(forecastOrderSDO.getModifierName())) {
            forecastOrder.setModifierName(forecastOrderSDO.getModifierName());
        }
        if (Objects.nonNull(forecastOrderSDO.getCustomerPurchaseOrderNo())) {
            forecastOrder.setCustomerPurchaseOrderNo(forecastOrderSDO.getCustomerPurchaseOrderNo());
        }
        if (Objects.nonNull(forecastOrderSDO.getCustomerPurchaseDate())) {
            forecastOrder.setCustomerPurchaseDate(forecastOrderSDO.getCustomerPurchaseDate());
        }
        if (Objects.nonNull(forecastOrderSDO.getSourceOrderNo())) {
            forecastOrder.setSourceOrderNo(forecastOrderSDO.getSourceOrderNo());
        }
        if (Objects.nonNull(forecastOrderSDO.getBusinessUnitCode())) {
            forecastOrder.setBusinessUnitCode(forecastOrderSDO.getBusinessUnitCode());
            forecastOrder.setBusinessUnitName(forecastOrderSDO.getBusinessUnitName());
        }
        if (Objects.nonNull(forecastOrderSDO.getBusinessUnitName())) {
            forecastOrder.setBusinessUnitName(forecastOrderSDO.getBusinessUnitName());
        }
        if (Objects.nonNull(forecastOrderSDO.getBusinessType())) {
            forecastOrder.setBusinessType(forecastOrderSDO.getBusinessType());
        }
        if (Objects.nonNull(forecastOrderSDO.getBusinessTypeName())) {
            forecastOrder.setBusinessTypeName(forecastOrderSDO.getBusinessTypeName());
        }
        if (Objects.nonNull(forecastOrderSDO.getFromSys())) {
            forecastOrder.setFromSys(forecastOrderSDO.getFromSys());
        }
        if (Objects.nonNull(forecastOrderSDO.getOrderTypeCode())) {
            forecastOrder.setOrderTypeCode(forecastOrderSDO.getOrderTypeCode());
        }
        if (Objects.nonNull(forecastOrderSDO.getOrderTypeName())) {
            forecastOrder.setOrderTypeName(forecastOrderSDO.getOrderTypeName());
        }
        if (Objects.nonNull(forecastOrderSDO.getOrderType())) {
            forecastOrder.setOrderType(forecastOrderSDO.getOrderType());
        }
        if (StringUtils.isNotBlank(forecastOrderSDO.getModifierCode())){
            forecastOrder.setModifierCode(forecastOrderSDO.getModifierCode());
        }
        forecastOrder.setStatus(forecastOrderSDO.getStatus());
        if (StringUtils.isNotBlank(forecastOrderSDO.getBizFeatures())){
            forecastOrder.setBizFeatures(forecastOrderSDO.getBizFeatures());
        }
        if (StringUtils.isNotBlank(forecastOrderSDO.getFulfillmentTypeCode())){
            forecastOrder.setFulfillmentTypeCode(forecastOrderSDO.getFulfillmentTypeCode());
        }
        if (StringUtils.isNotBlank(forecastOrderSDO.getFulfillmentPriority())){
            forecastOrder.setFulfillmentPriority(forecastOrderSDO.getFulfillmentPriority());
        }
        if (StringUtils.isNotBlank(forecastOrderSDO.getAutoFulfillment())){
            forecastOrder.setAutoFulfillment(forecastOrderSDO.getAutoFulfillment());
        }
        if (StringUtils.isNotBlank(forecastOrderSDO.getOrderTag())){
            forecastOrder.setOrderTag(forecastOrderSDO.getOrderTag());
        }
        forecastOrder.setReqNo(forecastOrderSDO.getReqNo());
        UpdateForecastOrderResponse updateForecastOrderResponse = forecastOrderService.updateForecastOrder(forecastOrder);
        Integer count = updateForecastOrderResponse.getCount();
        if (0 == count) {
            log.error("ForecastOrderWriteRepositoryImpl#modify_fail");
            log.error("ForecastOrderWriteRepositoryImpl|modify|更新主单失败日志", forecastOrder.getId());
            throw new RuntimeException("预报主单信息更新失败");
        }
        return true;
    }

    @Override
    public Boolean submit(ForecastOrderSDO forecastOrderSDO) {
        LoadForecastOrderRequest loadForecastOrderRequest = new LoadForecastOrderRequest();
        loadForecastOrderRequest.setId(forecastOrderSDO.getId());
        ForecastOrder forecastOrder = forecastOrderService.loadForecastOrder(loadForecastOrderRequest);
        forecastOrder.setId(Long.parseLong(forecastOrderSDO.getId()));
        forecastOrder.setStatus(forecastOrderSDO.getStatus());
        forecastOrder.setModifierId(forecastOrderSDO.getModifierId());
        forecastOrder.setModifierCode(forecastOrderSDO.getModifierCode());
        forecastOrder.setModifierName(forecastOrderSDO.getModifierName());
        forecastOrder.setVaildBeginTime(forecastOrderSDO.getVaildBeginTime());
        UpdateForecastOrderResponse updateForecastOrderResponse = forecastOrderService.updateForecastOrder(forecastOrder);
        Integer count = updateForecastOrderResponse.getCount();
        if (0 == count) {
            log.error("ForecastOrderWriteRepositoryImpl#submit_fail");
            return false;
        }
        return true;
    }

    @Override
    public Boolean modifyForecast(ForecastOrderSDO forecastOrderSDO) {
        LoadForecastOrderRequest loadForecastOrderRequest = new LoadForecastOrderRequest();
        loadForecastOrderRequest.setId(forecastOrderSDO.getId());
        ForecastOrder forecastOrder = forecastOrderService.loadForecastOrder(loadForecastOrderRequest);
        //客户收货地址
        if (StringUtils.isNotBlank(forecastOrderSDO.getReceiveAddressCode())) {
            forecastOrder.setReceiveAddressCode(forecastOrderSDO.getReceiveAddressCode());
        }
        if (StringUtils.isNotBlank(forecastOrderSDO.getCustomerReferenceNo())) {
            forecastOrder.setCustomerReferenceNo(forecastOrderSDO.getCustomerReferenceNo());
        }
        if (StringUtils.isNotBlank(forecastOrderSDO.getCustomerCode())) {
            forecastOrder.setCustomerCode(forecastOrderSDO.getCustomerCode());
        }
        //配送方式
        if (StringUtils.isNotBlank(forecastOrderSDO.getTransportModeCode())) {
            forecastOrder.setTransportModeCode(forecastOrderSDO.getTransportModeCode());
        }
        //单据原因
        if (StringUtils.isNotBlank(forecastOrderSDO.getCreateReason())) {
            forecastOrder.setCreateReason(forecastOrderSDO.getCreateReason());
        }
        //备注
        if (StringUtils.isNotBlank(forecastOrderSDO.getRemark())) {
            forecastOrder.setRemark(forecastOrderSDO.getRemark());
        }
        //补充备注
        if (StringUtils.isNotBlank(forecastOrderSDO.getAddRemark())) {
            forecastOrder.setAddRemark(forecastOrderSDO.getAddRemark());
        }
        //客户参考日期
        if (null != forecastOrderSDO.getCustomerExpectTDate()) {
            forecastOrder.setCustomerExpectTDate(forecastOrderSDO.getCustomerExpectTDate());
        }
        //发货仓库
        if (StringUtils.isNotBlank(forecastOrderSDO.getWareHouseName())) {
            forecastOrder.setWareHouseName(forecastOrderSDO.getWareHouseName());
        }
        if (null != forecastOrderSDO.getStatus()) {
            forecastOrder.setStatus(forecastOrderSDO.getStatus());
        }
        if (null != forecastOrderSDO.getRefuseStatus()) {
            forecastOrder.setRefuseStatus(forecastOrderSDO.getRefuseStatus());
        }
        if (Objects.nonNull(forecastOrderSDO.getModifierId())) {
            forecastOrder.setModifierId(forecastOrderSDO.getModifierId());
        }
        if (Objects.nonNull(forecastOrderSDO.getModifierName())) {
            forecastOrder.setModifierName(forecastOrderSDO.getModifierName());
        }
        if (Objects.nonNull(forecastOrderSDO.getModifierCode())) {
            forecastOrder.setModifierCode(forecastOrderSDO.getModifierCode());
        }
        UpdateForecastOrderResponse updateForecastOrderResponse = forecastOrderService.updateForecastOrder(forecastOrder);
        Integer count = updateForecastOrderResponse.getCount();
        if (0 == count) {
            log.error("ForecastOrderWriteRepositoryImpl#modifyForecast_fail");
            log.error("ForecastOrderWriteRepositoryImpl|modifyForecast|更新主单失败日志",forecastOrder.getId());
            throw new RuntimeException("预报主单信息更新失败");
        }
        return true;
    }

    @Override
    public void batchUpdateStatus(List<ForecastOrderSDO> forecastOrderSDOS, Modifiers modifier, List<SubmitForecastResponseDTO> responseDTOS) {
        List<String> forecastOrderIds = new ArrayList<>();
        List<ForecastOrderLineSDO> forecastOrderLineSDOList = new ArrayList<>();
        for (ForecastOrderSDO forecastOrderSDO : forecastOrderSDOS) {
            SubmitForecastResponseDTO forecastResponseDTO = new SubmitForecastResponseDTO();
            forecastResponseDTO.setSalesOrderNo(forecastOrderSDO.getOrderBizId());
            forecastResponseDTO.setSuccess(Boolean.TRUE);
            responseDTOS.add(forecastResponseDTO);

            // 预报订单主单列表
            forecastOrderIds.add(forecastOrderSDO.getId());

            // 批量更新预报单明细
            forecastOrderLineSDOList.addAll(forecastOrderSDO.getForecastOrderLineSDOList());
        }

        try {
            // 批量修改预报主单状态
            BatchUpdateStatusByIdsRequest batchUpdateStatusByIdsRequest = new BatchUpdateStatusByIdsRequest();
            batchUpdateStatusByIdsRequest.setIds(forecastOrderIds);
            batchUpdateStatusByIdsRequest.setStatus(ForecastStatusEnum.WAITING_FOR_REPLY.getNumValue());
            batchUpdateStatusByIdsRequest.setModifierId(modifier.getId());
            batchUpdateStatusByIdsRequest.setModifierName(modifier.getName());
            batchUpdateStatusByIdsRequest.setModifierCode(modifier.getWorkId());
            forecastOrderService.batchUpdateStatusByIds(batchUpdateStatusByIdsRequest);

            // 批量修改预报明细状态
            forecastOrderLineWriteRepository.batchUpdateStatusByIds(forecastOrderLineSDOList, modifier);
        } catch (Exception e) {
            log.error("预报单明细更新异常", e);
        }
    }
}
